﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Net;
using System.ServiceModel;
using System.Windows;
using System.Windows.Threading;

using SBPweb.RIA.Client.Common.Collections;
using SBPweb.RIA.Client.Common.Patterns;

using SBPweb.PlAzureNet.Shared.Library.Actors;

using SBPweb.PlAzureNet.Client.Library.Services.Tracking;

namespace SBPweb.PlAzureNet.Client.Library.Controllers
{

    public class TrackingServiceController
    {
        
        #region Fields

        private static SingletonHelper<TrackingServiceController> sh = new SingletonHelper<TrackingServiceController>();

        //private CookieContainer cookies = new CookieContainer();
        private TrackingServiceContractClient trackingService;

        #endregion

        #region Constructor logic

        public TrackingServiceController()
        {
            trackingService = new TrackingServiceContractClient();
            //dataService.CookieContainer = cookies;

            Dispatcher = Application.Current.RootVisual.Dispatcher;

            Action = new Services.Tracking.Action();

            trackingService.RegisterActionCompleted += new EventHandler<AsyncCompletedEventArgs>(trackingService_RegisterActionCompleted);
        }

        #endregion

        #region Properties

        public static TrackingServiceController Instance
        {
            get
            {
                return sh.Instance;
            }
        }

        internal Dispatcher Dispatcher
        {
            get;
            private set;
        }


        public IAction Action
        {
            get;
            private set;
        }

        #endregion

        #region Methods

        public void RegisterAction()
        {
            trackingService.RegisterActionAsync(Action as Services.Tracking.Action);
        }

        #endregion

        #region Event handlers

        private void trackingService_RegisterActionCompleted(object sender, AsyncCompletedEventArgs e)
        {
            if (e.Error != null)
            {
                throw e.Error;
            }
        }

        #endregion

    }

}